package com.gxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.common.base.RespResult;
import com.gxa.domain.dto.RolePageDto;
import com.gxa.domain.dto.WarehousePageDto;
import com.gxa.domain.entity.Role;
import com.gxa.domain.entity.Warehouse;
import com.gxa.mapper.RoleMapper;
import com.gxa.mapper.WarehouseMapper;
import com.gxa.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 万事顺意
 * Created with IntelliJ IDEA.
 * User: zwj
 * Date: 2024/07/1814:45
 * Description:
 */
@Service
public class WarehouseServiceImpl extends ServiceImpl<WarehouseMapper, Warehouse> implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;

    @Override
    public RespResult searchWarehouse(WarehousePageDto dto) {
        // 使用Warehouse类型的Page和QueryWrapper
        Page<Warehouse> page = new Page<>(dto.getPage(), dto.getLimit());
        QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();

        // 如果dto中的warehouseName不为空，则添加查询条件
        if (dto.getWarehouseName() != null && !dto.getWarehouseName().isEmpty()) {
            // 假设Warehouse实体中有一个warehouseName字段
            queryWrapper.like("warehouse_name", dto.getWarehouseName()); // 或者使用lambda表达式以避免硬编码字段名
            // 使用lambda表达式（如果MyBatis-Plus版本支持）
            // queryWrapper.lambda().like(Warehouse::getWarehouseName, dto.getWarehouseName());
        }

        // 调用warehouseMapper的selectPage方法，注意warehouseMapper应该是WarehouseMapper类型的
        Page<Warehouse> result = warehouseMapper.selectPage(page, queryWrapper);
        long total = result.getTotal();
        List<Warehouse> records = result.getRecords();

        // 返回查询结果，通常这里还会包括状态码和其他可能的响应信息
        return RespResult.success(records, total);
    }
}
